<template>
  <!-- 租客流水 -->
  <div class="room-turnover" style="height: 100%">
    <a-card size="small" style="height: 100%" v-if="hasOwner">
      <!-- 租客显示 -->
      <template v-if="currentOwner.id">
        <a-row type="flex" align="top" justify="space-between">
          <a-col>
            <a-dropdown v-if="roomOwnerList.length > 1" overlayClassName="hide-scroller-bar" :overlayStyle="{
              height: '500px',
              overflow: 'hidden auto',
              borderRadius: '10px'
            }">
              <div class="owner-name">
                <a-space>
                  <span>{{ currentOwner.owner_name }}</span>
                  <a-tag v-if="currentOwner.status == 0" color="#87d068">{{ currentOwner.owner_type }}</a-tag>
                  <a-tag v-else color="#777777">{{ currentOwner.owner_type }}</a-tag>
                  <DownOutlined />
                </a-space>
              </div>
              <template #overlay>
                <a-menu @click="onRoomOwnerChange" style="border: 1px solid #f0f0f0">
                  <a-menu-item v-for="item of roomOwnerList" :key="item.id">
                    <span>{{ item.owner_name }}</span>
                    <a-tag style="margin-left: 10px;" v-if="item.status == 0" color="#87d068">{{ item.owner_type }}</a-tag>
                    <a-tag style="margin-left: 10px;" v-else color="#777777">{{ item.owner_type }}</a-tag>
                  </a-menu-item>
                </a-menu>
              </template>
            </a-dropdown>
            <div class="owner-name" v-else>
              <span>{{ currentOwner.owner_name }}</span>
              <a-tag style="margin-left: 10px;" v-if="currentOwner.status == 0" color="#87d068">{{ currentOwner.owner_type }}</a-tag>
              <a-tag style="margin-left: 10px;" v-else color="#777777">{{ currentOwner.owner_type }}</a-tag>
            </div>
          </a-col>
          <a-col>
            <a-button type="link" @click="checkOwnerDetail">
              {{ $L("租客") }}详情
            </a-button>
          </a-col>
        </a-row>
        <template v-if="currentOwner.id">
          <BillTurnover
            :turnoverDetail="detail"
            turnoverType="tenant"
            :owner_id="currentOwner.id"
          />
        </template>
      </template>
    </a-card>
    <div class="no-owner" v-else>
      <a-empty description="当前房源暂无流水记录" />
    </div>

    <!-- 租客详情 -->
    <OwnerDetailDrawer
      :visible="ownerDetailVisible"
      :id="currentOwner.id"
      @cancel="onOwnerDetailClose"
      @closeAllModal="closeAllModal"
    />
  </div>
</template>
<script>
import { defineComponent, reactive, toRefs } from "vue";
import BillTurnover from "@/views/bill/BillTurnover/index.vue";
import request from "../../../common/utils/request";
import { DownOutlined } from "@ant-design/icons-vue";
import OwnerDetailDrawer from "../../../views/village/OwnerManage/modules/OwnerDetailDrawer.vue";

export default defineComponent({
  name: "RoomBillTurnover",
  props: {
    detail: {
      type: Object,
      default: () => ({}),
    },
  },
  components: {
    BillTurnover,
    DownOutlined,
    OwnerDetailDrawer,
  },
  setup(props, { emit }) {
    const state = reactive({
      hasOwner: true,
      roomOwnerList: [],
      currentOwner: {},
      ownerDetailVisible: false,
    });

    /** 查看租客详情 */
    const checkOwnerDetail = () => {
      state.ownerDetailVisible = true;
    };

    const onOwnerDetailClose = () => {
      state.ownerDetailVisible = false;
      getRoomOwnerList();
    };

    const closeAllModal = () => {
      emit("closeAllModal")
    };

    /** 选中租客 */
    const onRoomOwnerChange = (e) => {
      const { key } = e;
      state.currentOwner = state.roomOwnerList.find((it) => it.id == key);
    };

    /** 获取租客列表 */
    const getRoomOwnerList = () => {
      request
        .get("/village", "/project/room/getOwnerList", {
          build_id: props.detail.build_id,
          room_id: props.detail.id,
        })
        .then((res) => {
          if (res.list && res.list.length > 0) {
            state.hasOwner = true;
            state.roomOwnerList = res.list;
            let index = -1;
            if (state.currentOwner.id) {
              index = state.roomOwnerList.findIndex(
                (it) => it.id == state.currentOwner.id
              );
            }
            if (index == -1) {
              index = 0;
            }
            onRoomOwnerChange({ key: res.list[index].id });
          } else {
            state.hasOwner = false;
          }
        });
    };

    const init = () => {
      getRoomOwnerList();
    };

    init();

    return {
      ...toRefs(state),
      checkOwnerDetail,
      onOwnerDetailClose,
      onRoomOwnerChange,
      closeAllModal,
    };
  },
});
</script>

<style lang="less" scoped>
.no-owner {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.owner-name {
  font-size: 15px;
  font-weight: 600;
  color: rgba(0, 0, 0, 0.65);
  display: flex;
  align-items: center;
}
</style>
